<?php

class IPValidator extends CValidator
{

	public $pattern='/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/';
	public $allowEmpty=true;

	protected function validateAttribute($object,$attribute)
	{
		$value=$object->$attribute;
		if($this->allowEmpty && ($value===null || $value===''))
			return;
		$valid=preg_match($this->pattern,$value);
		if(!$valid)
		{
			$message=$this->message!==null?$this->message:Yii::t('cms','{attribute} is not a valid IP address.');
			$this->addError($object,$attribute,$message);
		}
	}
}
